package de.justphil.tcg.tcgserver.websockets.util;

import org.codehaus.jackson.map.ObjectMapper;

import net.jcip.annotations.ThreadSafe;

import de.justphil.tcg.tcgserver.commons.i18n.Error;
import de.justphil.tcg.tcgserver.websockets.messages.MessageSubject;
import de.justphil.tcg.tcgserver.util.JsonObjectProcessor;

@ThreadSafe
public class MessageWriter {
	// Jackson JSON Processor: shared resource, whose documentation says that it's thread-safe -> this class is also thread-safe
	private static final ObjectMapper mapper = JsonObjectProcessor.getJsonObjectMapper();
	
	public static String writeJson(Object messageObject) {
		try {
			return mapper.writeValueAsString(messageObject);
		}
		catch (Exception e) {
			e.printStackTrace();
			
			// Fall back
			return "{\"subject\":\"" + MessageSubject.ERROR + "\",\"msg\":\"" + Error.SERVER_ERROR_INTERNAL + "\"}";
		}
	}
}
